package Train;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import Main.Util;

/**
 * Fetch the train's information with train's id, and save it into databaase
 * 
 * ����г��ĳ��Σ�������ץȡ���г�����Ϣ�����������ֶ�
 * 
 * name						����
 * running_time				����ʱ��
 * starting_station			ʼ��վ
 * ending_station			�յ�վ
 * starting_time			����ʱ��
 * ending_time				��վʱ��
 * interval_day				���ڿ��
 * type						����(�տͣ����٣��ؿ� �ȵ�)
 * distance					���
 * hard_seat_price			Ӳ���
 * soft_seat_price			�����
 * hard_top_berth_price		Ӳ����
 * hard_center_berth_price	Ӳ����
 * hard_bottom_berth_price	Ӳ����
 * soft_top_berth_price		������
 * soft_bottom_berth_price	������
 * business_seat_price		������
 * first_seat_price			һ����
 * second_seat_price		������
 * out_update				������
 * 
 * ����Ϊһ����¼�����浽��ݿ�� train_line����
 * 
 * 
 * @author buju.hh
 *
 */
public class TrainLine {
	
	private String name;
	private String running_time;
	private String starting_station;
	private String ending_station;
	private String starting_time;
	private String ending_time;
	private String interval_day;
	private String type;
	private String distance;
	private String hard_seat_price;
	private String soft_seat_price;
	private String hard_top_berth_price;
	private String hard_center_berth_price;
	private String hard_bottom_berth_price;
	private String soft_top_berth_price;
	private String soft_bottom_berth_price;
	private String business_seat_price;
	private String first_seat_price;
	private String second_seat_price;
	private String out_update;
	
	private String trainInfo;
	private String NA = "NULL";
	
	public TrainLine(String trainInfo) {
		
		this.trainInfo= trainInfo;
		
		if((Main.JDBCDemo.DEBUG)){
			System.out.println("\n-------- DEBUG TrainLine START--------");
			//System.out.println(trainInfo);
		}
		
		get_name();
		get_running_time();
		get_starting_station();
		get_ending_station();
		get_starting_time();
		get_ending_time();
		get_interval_day();
		get_type();
		get_distance();
		get_hard_seat_price();
		get_soft_seat_price();
		get_berth_prices();
		get_soft_top_berth_price();
		get_soft_bottom_berth_price();
		get_business_seat_price();
		get_first_seat_price();
		get_second_seat_price();
		try {
			get_out_update();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(Main.JDBCDemo.DEBUG) System.out.println("-------- DEBUG TrainLine END--------\n\n");
	}
	
	public void get_name(){
		Pattern pattern = Pattern.compile("zhuti=[a-z|A-Z|0-9|/]*"); 
		Matcher matcher = pattern.matcher(this.trainInfo);

		if(matcher.find()){
			this.name = matcher.group().substring(6);
			if(Main.JDBCDemo.DEBUG) System.out.println("name:" + this.name);
		}else{
			if(Main.JDBCDemo.DEBUG) System.err.println("name:FAIL");
		}
	}
	
	public void get_running_time(){
		Pattern pattern = Pattern.compile("([0-9]*Сʱ[0-9]*��)|([0-9]*��)"); 
		Matcher matcher = pattern.matcher(this.trainInfo);

		if(matcher.find()){
			String running_time = matcher.group();
			
//			String hour, min;
//			if( running_time.indexOf("Сʱ") > -1){
//				hour = running_time.substring(0, running_time.indexOf("Сʱ"));
//				min = running_time.substring(running_time.indexOf("ʱ") + 1, running_time.length() - 1);
//			}else{
//				hour = "00";
//				min = running_time.substring(0, running_time.length() - 1);
//			}
//			
//			hour = (hour.length() < 2) ? ("0" + hour) : hour;
//			min = (min.length() < 2) ? ("0" + min) : min;
			this.running_time = this.formatTime(running_time); 
			
			if(Main.JDBCDemo.DEBUG) System.out.println("running_time:" + this.running_time);
		}else{
			if(Main.JDBCDemo.DEBUG) System.err.println("running_time:FAIL");
		}
	}
	
	public void get_starting_station(){
		Pattern pattern1 = Pattern.compile("ʼ��վ</td><tdalign=\"center\">.*?</td>"); 
		Matcher matcher1 = pattern1.matcher(this.trainInfo);

		if(matcher1.find()){
			String temp = matcher1.group();
			this.starting_station = temp.substring(26, temp.lastIndexOf("</td>"));
			if(Main.JDBCDemo.DEBUG) System.out.println("starting_station:" + this.starting_station);
		}else{
			if(Main.JDBCDemo.DEBUG) System.err.println("starting_station:FAIL");
		}
	}
	
	public void get_ending_station(){
		Pattern pattern1 = Pattern.compile("�յ�վ</td><tdalign=\"center\">.*?</td>"); 
		Matcher matcher1 = pattern1.matcher(this.trainInfo);

		if(matcher1.find()){
			String temp = matcher1.group();
			this.ending_station = temp.substring(26, temp.lastIndexOf("</td>"));
			if(Main.JDBCDemo.DEBUG) System.out.println("ending_station:" + this.ending_station);
		}else{
			if(Main.JDBCDemo.DEBUG) System.err.println("ending_station:FAIL");
		}
	}
	
	public void get_starting_time(){
		Pattern pattern1 = Pattern.compile("����ʱ��</td><tdalign=\"center\">.*?</td>"); 
		Matcher matcher1 = pattern1.matcher(this.trainInfo);

		if(matcher1.find()){
			String temp = matcher1.group();
			this.starting_time = temp.substring(27, temp.lastIndexOf("</td>"));
			
			// Add :00 to the end of starting_time 
			this.starting_time = this.starting_time + ":00";
			
			if(Main.JDBCDemo.DEBUG) System.out.println("starting_time:" + this.starting_time);
		}else{
			if(Main.JDBCDemo.DEBUG) System.err.println("starting_time:FAIL");
		}
	}
	
	public void get_ending_time(){
		Pattern pattern1 = Pattern.compile("��վʱ��</td><tdalign=\"center\">.*?</td>"); 
		Matcher matcher1 = pattern1.matcher(this.trainInfo);

		if(matcher1.find()){
			String temp = matcher1.group();
			this.ending_time = temp.substring(27, temp.lastIndexOf("</td>"));
			
			// Add :00 to the end of ending_time 
			this.ending_time = this.ending_time + ":00";
			
			if(Main.JDBCDemo.DEBUG) System.out.println("ending_time:" + this.ending_time);
		}else{
			if(Main.JDBCDemo.DEBUG) System.err.println("ending_time:FAIL");
		}
	}
	
	public void get_interval_day(){
		Pattern pattern1 = Pattern.compile("��[0-9]*�쵽"); 
		Matcher matcher1 = pattern1.matcher(this.trainInfo);

		if(matcher1.find()){
			String temp = matcher1.group();
			Pattern pattern2 = Pattern.compile("[0-9]"); 
			Matcher matcher2 = pattern2.matcher(temp);
			
			if(matcher2.find()){
				this.interval_day = matcher2.group();
			}
			
			if(Main.JDBCDemo.DEBUG) System.out.println("interval_day:" + this.interval_day);
		}else{
			if(Main.JDBCDemo.DEBUG) System.err.println("interval_day:FAIL");
		}
	}
	
	public void get_type(){
		Pattern pattern1 = Pattern.compile("����</td><tdalign=\"center\">.*?</td>"); 
		Matcher matcher1 = pattern1.matcher(this.trainInfo);

		if(matcher1.find()){
			String temp = matcher1.group();
			this.type = temp.substring(25, temp.lastIndexOf("</td>"));
			if(Main.JDBCDemo.DEBUG) System.out.println("type:" + this.type);
		}else{
			if(Main.JDBCDemo.DEBUG) System.err.println("type:FAIL");
		}
	}
	
	public void get_distance(){
		Pattern pattern1 = Pattern.compile("ȫ��</td>.*?</td>"); 
		Matcher matcher1 = pattern1.matcher(this.trainInfo);

		if(matcher1.find()){
			String temp = matcher1.group();
			this.distance = temp.substring(25, temp.lastIndexOf("����"));
			if(Main.JDBCDemo.DEBUG) System.out.println("distance:" + this.distance);
		}else{
			if(Main.JDBCDemo.DEBUG) System.err.println("distance:FAIL");
		}
	}
	
	public void get_hard_seat_price(){
		Pattern pattern1 = Pattern.compile("Ӳ��</td>.*?</td>"); 
		Matcher matcher1 = pattern1.matcher(this.trainInfo);

		if(matcher1.find()){
			String temp = matcher1.group();
			this.hard_seat_price = temp.substring(42, temp.lastIndexOf("</td>"));
			
			// Add 0 to the end of price
			this.hard_seat_price = this.formatPrice(this.hard_seat_price);
//			if(!this.hard_seat_price.equals("-"))
//				this.hard_seat_price = this.hard_seat_price + "0";
//			else
//				this.hard_seat_price = NA;
			
			if(Main.JDBCDemo.DEBUG) System.out.println("hard_seat_price:" + this.hard_seat_price);
		}else{
			this.hard_seat_price = NA;
			if(Main.JDBCDemo.DEBUG) System.out.println("hard_seat_price:" + NA);
		}
	}
	
	public void get_soft_seat_price(){
		Pattern pattern1 = Pattern.compile("����</td>.*?</td>"); 
		Matcher matcher1 = pattern1.matcher(this.trainInfo);

		if(matcher1.find()){
			String temp = matcher1.group();
			this.soft_seat_price = temp.substring(25, temp.lastIndexOf("</td>"));
			
			// Add 0 to the end of price
			this.soft_seat_price = this.formatPrice(this.soft_seat_price);
//			if(!this.soft_seat_price.equals("-"))
//				this.soft_seat_price = this.soft_seat_price + "0";
//			else
//				this.soft_seat_price = NA;
			
			if(Main.JDBCDemo.DEBUG) System.out.println("soft_seat_price:" + this.soft_seat_price);
		}else{
			this.soft_seat_price = NA;
			if(Main.JDBCDemo.DEBUG) System.out.println("soft_seat_price:" + NA);
		}
	}
	
	public void get_berth_prices(){
		Pattern pattern1 = Pattern.compile("Ӳ����/��/��</td>.*?</td>"); 
		Matcher matcher1 = pattern1.matcher(this.trainInfo);

		if(matcher1.find()){
			String temp = matcher1.group();
			String berth_prices = temp.substring(58, temp.lastIndexOf("</td>"));

			String[] priceArr = berth_prices.split("/");
			String hard_top_berth_price = priceArr[0];
			String hard_center_berth_price = priceArr[1];
			String hard_bottom_berth_price = priceArr[2];
			
			// Add 0 to the end of price
			this.hard_top_berth_price = this.formatPrice(hard_top_berth_price);
			this.hard_center_berth_price = this.formatPrice(hard_center_berth_price);
			this.hard_bottom_berth_price = this.formatPrice(hard_bottom_berth_price);
//			if(!this.hard_top_berth_price.equals("-"))
//				this.hard_top_berth_price = this.hard_top_berth_price + "0";
//			else
//				this.hard_top_berth_price = NA;
//			
//			if(!this.hard_center_berth_price.equals("-"))
//				this.hard_center_berth_price = this.hard_center_berth_price + "0";
//			else
//				this.hard_center_berth_price = NA;
//			
//			if(!this.hard_bottom_berth_price.equals("-"))
//				this.hard_bottom_berth_price = this.hard_bottom_berth_price + "0";
//			else
//				this.hard_bottom_berth_price = NA;
			
			if(Main.JDBCDemo.DEBUG) System.out.println("hard_top_berth_price:" + this.hard_top_berth_price);
			if(Main.JDBCDemo.DEBUG) System.out.println("hard_center_berth_price:" + this.hard_center_berth_price);
			if(Main.JDBCDemo.DEBUG) System.out.println("hard_bottom_berth_price:" + this.hard_bottom_berth_price);
		}else{
			this.hard_top_berth_price = NA;
			this.hard_center_berth_price = NA;
			this.hard_bottom_berth_price = NA;
			if(Main.JDBCDemo.DEBUG) System.out.println("hard_top_berth_price:" + NA);
			if(Main.JDBCDemo.DEBUG) System.out.println("hard_center_berth_price:" + NA);
			if(Main.JDBCDemo.DEBUG) System.out.println("hard_bottom_berth_price:" + NA);
		}
	}

	public void get_soft_top_berth_price(){
		Pattern pattern1 = Pattern.compile("������</td>.*?</td>"); 
		Matcher matcher1 = pattern1.matcher(this.trainInfo);

		if(matcher1.find()){
			String temp = matcher1.group();
			this.soft_top_berth_price = temp.substring(43, temp.lastIndexOf("</td>"));
			
			// Add 0 to the end of price
			this.soft_top_berth_price = this.formatPrice(this.soft_top_berth_price);
//			if(!this.soft_top_berth_price.equals("-"))
//				this.soft_top_berth_price = this.soft_top_berth_price + "0";
//			else
//				this.soft_top_berth_price = NA;
			
			if(Main.JDBCDemo.DEBUG) System.out.println("soft_top_berth_price:" + this.soft_top_berth_price);
		}else{
			this.soft_top_berth_price = NA;
			if(Main.JDBCDemo.DEBUG) System.out.println("soft_top_berth_price:" + NA);
		}
	}
	
	public void get_soft_bottom_berth_price(){
		Pattern pattern1 = Pattern.compile("������</td>.*?</td>"); 
		Matcher matcher1 = pattern1.matcher(this.trainInfo);

		if(matcher1.find()){
			String temp = matcher1.group();
			this.soft_bottom_berth_price = temp.substring(43, temp.lastIndexOf("</td>"));
			
			// Add 0 to the end of price
			this.soft_bottom_berth_price = this.formatPrice(this.soft_bottom_berth_price);
//			if(!this.soft_bottom_berth_price.equals("-"))
//				this.soft_bottom_berth_price = this.soft_bottom_berth_price + "0";
//			else
//				this.soft_bottom_berth_price = NA;
						
			if(Main.JDBCDemo.DEBUG) System.out.println("soft_bottom_berth_price:" + this.soft_bottom_berth_price);
		}else{
			this.soft_bottom_berth_price = NA;
			if(Main.JDBCDemo.DEBUG) System.out.println("soft_bottom_berth_price:" + NA);
		}
	}
	
	public void get_business_seat_price(){ 
		Pattern pattern1 = Pattern.compile("�ص���/������</td>.*?</td>"); 
		Matcher matcher1 = pattern1.matcher(this.trainInfo);

		if(matcher1.find()){
			String temp = matcher1.group();
			this.business_seat_price = temp.substring(58, temp.lastIndexOf("</td>"));
			
			// Add 0 to the end of price
			this.business_seat_price = this.formatPrice(this.business_seat_price);
//			if(!this.business_seat_price.equals("-"))
//				this.business_seat_price = this.business_seat_price + "0";
//			else
//				this.business_seat_price = NA;
						
			if(Main.JDBCDemo.DEBUG) System.out.println("business_seat_price:" + this.business_seat_price);
		}else{
			this.business_seat_price = NA;
			if(Main.JDBCDemo.DEBUG) System.out.println("business_seat_price:" + NA);
		}
	}
	
	public void get_first_seat_price(){
		Pattern pattern1 = Pattern.compile("һ��</td>.*?</td>"); 
		Matcher matcher1 = pattern1.matcher(this.trainInfo);

		if(matcher1.find()){
			String temp = matcher1.group();
			this.first_seat_price = temp.substring(53, temp.lastIndexOf("</td>"));
			
			// Add 0 to the end of price
			this.first_seat_price = this.formatPrice(this.first_seat_price);
//			if(!this.first_seat_price.equals("-"))
//				this.first_seat_price = this.first_seat_price + "0";
//			else
//				this.first_seat_price = NA;
					
			if(Main.JDBCDemo.DEBUG) System.out.println("first_seat_price:" + this.first_seat_price);
		}else{
			this.first_seat_price = NA;
			if(Main.JDBCDemo.DEBUG) System.out.println("first_seat_price:" + NA);
		}
	}
	
	public void get_second_seat_price(){
		Pattern pattern1 = Pattern.compile("����</td>.*?</td>"); 
		Matcher matcher1 = pattern1.matcher(this.trainInfo);

		if(matcher1.find()){
			String temp = matcher1.group();
			this.second_seat_price = temp.substring(53, temp.lastIndexOf("</td>"));
			
			// Add 0 to the end of price
			this.second_seat_price = this.formatPrice(this.second_seat_price);
			
			if(Main.JDBCDemo.DEBUG) System.out.println("second_seat_price:" + this.second_seat_price);
		}else{
			this.second_seat_price = NA;
			if(Main.JDBCDemo.DEBUG) System.out.println("second_seat_price:" + NA);
		}
	}
	
	public void get_out_update() throws ParseException{
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd"); 
		
		Pattern pattern1 = Pattern.compile("������.*?</td>"); 
		Matcher matcher1 = pattern1.matcher(this.trainInfo);

		if(matcher1.find()){
			String temp = matcher1.group();
			Pattern pattern2 = Pattern.compile("[0-9]*-[0-9]*-[0-9]*"); 
			Matcher matcher2 = pattern2.matcher(temp);
			
			if(matcher2.find()){		
				Date aDate = format.parse(matcher2.group());
				this.out_update = format.format(aDate);
			}
			
			if(Main.JDBCDemo.DEBUG) System.out.println("out_update:" + this.out_update);
		}else{
			if(Main.JDBCDemo.DEBUG) System.err.println("out_update:FAIL");
		}
	}
	
	public void writeToDatabase(){
		ArrayList<String> data = new ArrayList<String>();
		data.add(name);
		data.add(running_time);
		data.add(starting_station);
		data.add(ending_station);
		data.add(starting_time);
		data.add(ending_time);
		data.add(interval_day);
		data.add(type);
		data.add(distance);
		data.add(hard_seat_price);
		data.add(soft_seat_price);
		data.add(hard_top_berth_price);
		data.add(hard_center_berth_price);
		data.add(hard_bottom_berth_price);
		data.add(soft_top_berth_price);
		data.add(soft_bottom_berth_price);
		data.add(business_seat_price);
		data.add(first_seat_price);
		data.add(second_seat_price);
		data.add(out_update);
		
		int ret = Util.writeToLocalDB_bus_line(data);
		
		if(ret == 1){
			System.out.println(Util.getCurrentTime() + "	Write database [" + name + "] - Done");
		}else{
			System.out.println(Util.getCurrentTime() + "	Write database [" + name + "] - Fail");
		}
	}
	
	private String formatTime(String time){
		String hour, min;
		if( time.indexOf("Сʱ") > -1){
			hour = time.substring(0, time.indexOf("Сʱ"));
			min = time.substring(time.indexOf("ʱ") + 1, time.length() - 1);
		}else{
			hour = "00";
			min = time.substring(0, time.length() - 1);
		}
		
		hour = (hour.length() < 2) ? ("0" + hour) : hour;
		min = (min.length() < 2) ? ("0" + min) : min;
		
		return hour + ":" + min + ":" + "00";
	}
	
	private String formatPrice(String time){
		String afterFormat = "";		
		
		if(time.equals("0")){
			afterFormat = time;
		}else if(!time.equals("-")){
			if(time.indexOf(".") > -1){
				String[] a = time.split("\\.");
				afterFormat = a[0] + a[1];
			}else if(time.equals("")){
				afterFormat = NA;
			}else{
				afterFormat = time + "0";
			}
			
		}else
			afterFormat = NA;
		
		return afterFormat;
	}
	
}
